package com.wanlong.admin.controller;

import com.wanlong.common.result.ResponseResult;
import com.wanlong.common.result.VueResult;
import com.wanlong.common.web.AuthUserInfo;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@Controller
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping(value = "/admin/sign")
public class LoginController {

    @RequestMapping(value = "/login" , method = RequestMethod.POST)
    @ResponseBody
    public VueResult login(@RequestBody JSONObject json ){
        //验证用户名及密码
        UsernamePasswordToken loginToken = new UsernamePasswordToken(json.getString("username"),json.getString("password"),false);
        Subject subject = SecurityUtils.getSubject();
        try{
            subject.login(loginToken);
        }catch (IncorrectCredentialsException e){
            //密码错误输出
            return VueResult.failure("密码错误");
            //return "密码错误";
        }catch (ExcessiveAttemptsException e){
            //失败次数过多提示
            return VueResult.failure("失败次数过多");
            //return "失败次数过多";
        }catch (LockedAccountException e){
            //账户锁定提示
            return VueResult.failure("账号锁定");
            //return "账号锁定";
        }catch (DisabledAccountException e){
            //账号禁用提示
            return VueResult.failure("账号禁用");
            //return "账号禁用";
        }catch (ExpiredCredentialsException e){
            //账号过期提示
            return VueResult.failure("账号已过期");
            //return "账号已过期";
        }catch (UnknownAccountException e){
            //账号不存在提示
            return VueResult.failure("账号不存在");
            //return "账号不存在";
        }catch (UnauthorizedException e){
            //没有权限提示
            return VueResult.failure("权限不足");
            //return "权限不足";
        }catch (Exception e){
            //其他信息提示
            return VueResult.failure("其他一般信息");
            //return "其他一般信息";
        }
        System.out.println("登陆成功="+loginToken.toString());
        String token = "admin";
        VueResult result= new VueResult();
        result.setToken("admin");
        result.setCode("0000");
        result.setMessage("操作成功");
        return result;
        //return "{\"code\":20000,\"token\":\"admin\"}";
    }
    @RequestMapping(value = "/getUserInfo" , method = RequestMethod.GET,produces = "application/json;charset=UTF-8")//,produces = "application/json;charset=UTF-8"
    @ResponseBody
    public VueResult getUserInfo(@RequestParam("token")String token ){
        VueResult result = new VueResult();
        result.setCode("0000");
        result.setMessage("操作成功");
        result.setName("李刚");
        result.setToken("admin");
        result.setIntroduction("超级用户");
        List<String> roles = new ArrayList<>();
        roles.add("admin");
        result.setRoles(roles);
        result.setAvatar(VueResult.DEFAULT_IMG);
        return result;

        //return "{\"code\":20000,\"roles\":[\"admin\"],\"token\":\"admin\",\"\":\"我是超级管理员\",\"avatar\":\"\",\"name\":\"李刚\"}";
    }


    @RequestMapping(value = "/logout" , method = RequestMethod.POST)
    @ResponseBody
    public String loginout(){
        return "success";
    }
}
